package cn.liccshop.core.tmp.controller;


import cn.liccshop.core.entity.TmpTest;
import cn.liccshop.core.tmp.service.ITmpTestService;
import cn.liccshop.core.utils.GoodsRedisUtil;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.cache.CommonCacheUtil;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.MenuListOfAdmin;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.entity.UserElement;
import cn.liccshop.sys.exception.MySystemException;
import cn.liccshop.sys.utils.RandomNumberCode;
import cn.liccshop.sys.zhifubao.MyPay;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.liccshop.sys.base.MyActionController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description  前端控制器
 *
 * @author LiCC
 * @since 2019-03-16
 */
@RestController
@RequestMapping("/tmpTest")
public class TmpTestController extends MyActionController<TmpTest> {

    @Autowired
    private ITmpTestService liTmpTestService;
    @Autowired
    private GoodsRedisUtil goodsRedisUtil;
    @Autowired
    private CommonCacheUtil commonCacheUtil;
    @Autowired
    private MyPay myPay;

    @Override
    protected IService<TmpTest> getLiService(){
        return liTmpTestService;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody Map<Object, Object> data){
        Map<String, String> map = new HashMap<>();
        map.put("token","LICC-SDFOSD235423JSKJD");
        return new Result().setCode(Constants.OK).setData(map).setMessage("保存成功");
    }
    @RequestMapping("/info")
    @ResponseBody
    public Result info(){
//        MenuListOfAdmin menuListOfAdmin = new MenuListOfAdmin();
//        List<Map<String, String>> perms = new ArrayList<>();
//        Map<String, String> perm = new HashMap<>();
//        perm.put("name", "菜单1");
//        perm.put("val", "*");
//        perms.add(perm);
//        menuListOfAdmin.setName("licc-name").setNick("licc-nick").setRoles("licc-roles").setPerms(perms);
//        return new Result().setCode(Constants.OK).setData(menuListOfAdmin).setMessage("获取成功");
        return new Result().setCode(Constants.OK).setData(null).setMessage("获取成功");
    }

    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(){
        UserElement token = getUserInfoByToken();
        System.out.println("token --- " + token);
        return new Result().setCode(Constants.OK).setMessage("退出成功");
    }

    @RequestMapping("/test")
    @ResponseBody
    public Result test(@RequestBody Map<String, Integer> map){
        goodsRedisUtil.goodsHot(map);
        return new Result().setCode(Constants.OK).setData(commonCacheUtil.getCacheForGoodsOfHot()).setMessage("操作成功");
    }

    @RequestMapping("/testPay")
    @ResponseBody
    public Result testPay(@RequestBody Map<String, Object> map) throws MySystemException {
        BigDecimal price = new BigDecimal((String) map.get("price"));
        String orderSn = RandomNumberCode.getOrderSN();
        System.out.println(orderSn);
        String html = myPay.pay(orderSn, price.toString());
        return new Result().setCode(Constants.OK).setData(html).setMessage("操作成功");
    }

    @RequestMapping("/testVerificationPay")
    @ResponseBody
    public Result testVerificationPay(@RequestBody Map<String, Object> map) throws MySystemException {
        String orderSn = (String) map.get("orderSn");
        Map<String, Object> data = myPay.verificationPay(orderSn);
        return new Result().setCode(Constants.OK).setData(data).setMessage((boolean)data.get("succ")?"支付成功":"支付失败");
    }
    @RequestMapping("/refund")
    @ResponseBody
    public Result refund(@RequestBody Map<String, Object> map) throws MySystemException {
        String orderSn = (String) map.get("orderSn");
        String amount = (String) map.get("amount");
        Map<String, Object> result = myPay.refund(orderSn, amount);
        Integer isSuccess = (Integer) result.get("code");
        return new Result().setCode(Constants.OK).setMessage(isSuccess==1?"退款成功":result.get("message"));
    }
}

